$(document).ready(function() {
	$Base.isLoginManage(getMemberList);
});

function getMemberList() {
	var name = $.trim($('#search_name').val());
	var url = '/json?action=MANAGE_QUERY_T_MEMBERBY_PAGER_ACTION&MEMBER_ACCOUNT=' + name;
	var current = $('#pageId').val();
	$UI.changePage(url,10,'T_MEMBER','#list',printRes,'#page','<tr><td colspan="8"><div style="text-align:center">暂无数据</div></td></tr>',current,null,'#pageId');
}

function printRes(obj,result,i,n,total,current){
	var trStr = [
		'<tr '+ (i%2 == 0 ? "" : "class=\'alt\'") +'>',
		'<td>'+ obj.MEMBER_ACCOUNT +'</td>',
		'<td>'+ (obj.SEX == '1' ? '男' :  (obj.SEX == '0' ? '女' : '未知')) +'</td>',
		'<td>'+ obj.MEMBER_NAME +'</td>',
		'<td>'+ obj.MOBILE +'</td>',
		'<td>'+ obj.AVAILABLE_POINTS +'</td>',
		'<td>'+ (obj.MEMBER_STATUS == '1' ? '正常' :  (obj.MEMBER_STATUS == '2' ? '冻结' : '未知')) +'</td>',
		'<td>'+ $Util.toChar(obj.CREATE_DATE, 'yyyy-mm-dd') +'</td>',
		'<td>',
		'<a href="#">修改</a> ',
		'<a href="javascript:freeze('+obj.MEMBER_ID+',\''+obj.MEMBER_ACCOUNT+'\','+obj.MEMBER_STATUS+')">'+(obj.MEMBER_STATUS == '1' ? '冻结' :  (obj.MEMBER_STATUS == '2' ? '解冻' : '未知'))+'</a> ',
		'<a href="javascript:resetPassword('+obj.MEMBER_ID+',\''+obj.MEMBER_ACCOUNT+'\')">重置密码</a> ',
		'<a href="javascript:deleteMember('+obj.MEMBER_ID+',\''+obj.MEMBER_ACCOUNT+'\')">删除</a>',
		'</td>',
		'</tr>'
	].join('');
	$(result).append(trStr);
}

function freeze(id, name, status){
	var oper = "";
	if(status == 1){
		status = 2;
		oper = "冻结";
	}
	else if(status == 2){
		status = 1;
		oper = "解冻";
	}
	var btnList = [{
		id: 'ok',
		name: '确定',
		click: function(){
			var url = '/json?action=MANAGE_UPDATE_MEMBER_STATUS_ACTION&MEMBER_ID='+id + "&STATUS="+status;
			$.post(url,{},function(data){
				if(data.success){
					getMemberList();
					$.zxxbox.msg(oper+"成功！", 1000, 'success');
				} else {
					$.zxxbox.msg(data.errors.errmsg, 2000, 'error');
				}
			},'json');
		}
	},{
		id: 'cancel',
		name: '取消',
		css: 'cancel_btn',
		click: function(){
			$.zxxbox.hide();
		}
	}];
	$.zxxbox.ask("您确定要"+oper+"会员【" + name + "】吗？", btnList, {title: "操作确认"});
}

function resetPassword(id, name){
	var btnList = [{
		id: 'ok',
		name: '确定',
		click: function(){
			var url = '/json?action=MANAGE_RESET_PASSWORD_ACTION&MEMBER_ID='+id;
			$.post(url,{},function(data){
				if(data.success){
					getMemberList();
					$.zxxbox.msg("重置密码成功！", 1000, 'success');
				} else {
					$.zxxbox.msg(data.errors.errmsg, 2000, 'error');
				}
			},'json');
		}
	},{
		id: 'cancel',
		name: '取消',
		css: 'cancel_btn',
		click: function(){
			$.zxxbox.hide();
		}
	}];
	$.zxxbox.ask("您确定要重置会员【" + name + "】的密码吗？<br/>初始密码为：88888888", btnList, {title: "操作确认"});
}

function deleteMember(id, name){
	var btnList = [{
		id: 'ok',
		name: '确定',
		click: function(){
			var url = '/json?action=MANAGE_DELETE_MEMBER_ACTION&MEMBER_ID='+id;
			$.post(url,{},function(data){
				if(data.success){
					getMemberList();
					$.zxxbox.msg("删除成功！", 1000, 'success');
				} else {
					$.zxxbox.msg(data.errors.errmsg, 2000, 'error');
				}
			},'json');
		}
	},{
		id: 'cancel',
		name: '取消',
		css: 'cancel_btn',
		click: function(){
			$.zxxbox.hide();
		}
	}];
	$.zxxbox.ask("您确定要删除会员【" + name + "】吗？", btnList, {title: "操作确认"});
}
